package cn.zh.log;

import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.util.logging.Handler;

/**
 * 自定义日志拦截器
 *
 * @author 白羊
 * @date 2021/12/23
 */
public class MyLogInterceptor extends HandlerInterceptorAdapter {
    private static final ThreadLocal<Long> startTimeThreadLocal = new ThreadLocal<>();

    //在Controller方法执行之前
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        Method method = handlerMethod.getMethod();//获得被拦截的方法对象
        MyLog annotation = method.getAnnotation(MyLog.class);//获得方法上的注解
        if (annotation != null) {
            //说明当前拦截器的方法上加入了MyLog注解
            long startTime = System.currentTimeMillis();
            startTimeThreadLocal.set(startTime);
        }
        return super.preHandle(request, response, handler);
    }

    //在Controller方法执行之后
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        Method method = handlerMethod.getMethod();//获得被拦截的方法对象
        MyLog annotation = method.getAnnotation(MyLog.class);//获得方法上的注解
        if (annotation != null) {
            //说明当前拦截器的方法上加入了MyLog注解
            Long startTime = startTimeThreadLocal.get();
            long endTime = System.currentTimeMillis();
            long optTime = endTime - startTime;
            String requestURI = request.getRequestURI();
            String methodName = method.getDeclaringClass().getName() + "." + method.getName();
            String desc = annotation.desc();

            System.out.println("请求uri：" + requestURI);
            System.out.println("请求方法名：" + methodName);
            System.out.println("方法描述：" + desc);
            System.out.println("方法执行时间：" + optTime + "ms");

        }
        super.postHandle(request, response, handler, modelAndView);
    }
}
